package com.cares.core.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.mapper.JsonMapper;
import org.springside.modules.utils.DateProvider;

import com.cares.core.base.mybatis.BaseMybatisDao;
import com.cares.core.common.service.DicService;
import com.cares.core.mng.service.ShiroDbRealm.ShiroUser;
import com.cares.entity.common.Dic;

/**
 * 基础oracle Service抽象类 使用TourGroup数据源及回滚
 * 
 * @author xmarsx
 * @create 2013-7-24 下午7:27:33
 */

@Component
@Transactional(value = "CORE-DB", rollbackFor = Exception.class)
public class BaseService {

	protected BaseMybatisDao baseDao;

	private final JsonMapper jsonMapper = JsonMapper.nonDefaultMapper();
	
	protected DateProvider dateProvider = DateProvider.DEFAULT;
	
	@Resource(name="dicService")
	private DicService dicService;

	/**
	 * 
	 * @param sequence
	 * @return
	 */
	public Long getSequence(String sequence) {
		return baseDao.getSequence(sequence);
	}
	
	public Long getTableKey(String tableName) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("tableName", tableName);
		params.put("keyValue", 0);
		baseDao.getTableKey(params);
		return (Long) params.get("keyValue");
	}

	/**
	 * 取出Shiro中的当前用户LoginName.
	 */
	public String getCurrentUserName() {
		ShiroUser user = getCurrentUser();
		if (user == null)
			return "";
		return user.getLoginName();
	}
	
	public String getCurrentUserNm() {
		ShiroUser user = getCurrentUser();
		if (user == null)
			return "";
		return user.getName();
	}

	public String getCurrentUserOrgCode() {
		ShiroUser user = getCurrentUser();
		if (user == null)
			return null;
		return user.getOrgCode();
	}

	public Long getCurrentUserOrgId() {
		ShiroUser user = getCurrentUser();
		if (user == null)
			return null;
		return user.getOrgId();
	}
	
	public String getCurrentUserOrgPath() {
		ShiroUser user = getCurrentUser();
		if (user == null)
			return null;
		return user.getOrgPath();
	}
	
	public String getCurrentUserOrgType() {
		ShiroUser user = getCurrentUser();
		if (user == null)
			return null;
		return user.getOrgType();
	}

	/**
	 * 取出Shiro中的当前用户.
	 */
	public ShiroUser getCurrentUser() {
		if (null == SecurityUtils.getSubject())
			return null;
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user;
	}

	public Subject getSubject() {
		if (null == SecurityUtils.getSubject())
			return null;
		return SecurityUtils.getSubject();
	}

	public boolean isPermmit(String authCode) {
		if (null == SecurityUtils.getSubject())
			return false;
		return SecurityUtils.getSubject().isPermitted(authCode);
	}

	/**
	 * 根据orgPath 生成临时表数据：含当前机构的所有子机构
	 * 临时表：TE_ORGNIZATION
	 * @param orgPath
	 */
	public void updateFilterOrgnization(String orgPath) {
		baseDao.updateFilterOrgnization(orgPath);
	}
	
	/**
	 * 根据字典代码查询字典列表
	 * @param value
	 * @param mode
	 * @return
	 */
	public List<Dic> getDicList4Combobox(String value, String mode) {
		return dicService.getDicList4Combox(value, mode);
	}
	
	/**
	 * 获取8位数字订单编号
	 * @return
	 */
	public String updateOrderNo() {
		List<Dic> list = dicService.getDicListByCode("order_no");
		//返回list必然不为空
		Dic dic = list.get(0);
		//获取当前订单编号
		String result = dic.getDicValue2();
		//订单编号更新
		Integer orderNo = Integer.parseInt(result);
		orderNo++;
		dic.setDicValue2(""+orderNo);
		dicService.update(dic);
		
		return result;
	}
	
	/**
	 * 根据字典子项ID查找所有父字典名称
	 * @param id
	 * @return
	 */
	public String getParentBusiness(Long id) {
		return dicService.getParentBusiness(id);
	}
	
	/**
	 * 根据字典项ID查找字典项名称
	 * @param id
	 * @return
	 */
	public String getBusiness(Long id) {
		Dic dic = dicService.get(id);
		if(dic!=null) {
			return dic.getDicName();
		}
		return "";
	}

}
